<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\RunAction;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
class TestController extends Controller
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('模版');//这里是页面标题
            $content->description('列表');//这里是详情描述

            $content->body($this->grid());//指向grid方法显示表格

        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('模版列表');
            $content->description('新增');
            $content->body($this->form());//调用form方法，显示表单

        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('模版列表');
            $content->description();
            $content->body($this->form()->edit($id));//将id传给form，form的模型就是以id为查询条件的
        });
    }


    public function show($id)

    {

        $show = new Show(Article::findOrFail($id));

        $show->id('ID');

        $show->title('Title');

        $show->content('Prompt');
        $show->tokens('Tokens');
        $show->remark('Remark');

        $show->created_at('created_at');

        $show->updated_at('updated_at');

        return $show;

    }


    protected function grid()
    {//页面显示的表格

        return Admin::grid(Article::class, function (Grid $grid) {
//grid显示表格内容，$grid->数据库中相应的字段（‘在页面上显示的名称’）->其他方法();或者$grid->column（‘数据库中相应的字段’，‘在页面上显示的名称’）->其他方法();

            $grid->id('ID')->sortable();

            $grid->title('Title');

            $grid->remark('Remark');

            // 添加自定义按钮
            // 添加自定义列
            $grid->column('run', __('Run'))->display(function () {
                return (new RunAction())->render();
            });
//            $grid->actions(function ($actions) {
//                $actions->add(new RunAction());
//            });

            // $grid->disableExport();//禁用导出数据按钮

            // $grid->filter(function ($filter) {

            //   $filter->disableIdFilter();//禁用查询过滤器

            // $filter->like('name', '名称'));//用名称作为条件模糊查询

        });
    }


    protected function form()
    {//点击新增或者修改进入form表单

        return Admin::form(Article::class, function (Form $form) {

            $form->text('title','Title')->rules("required");//required为名称规则，具体信息的定义在recourse=>lang=>zh_CN=>validation.php文件中
            $form->textarea('content','Prompt')->rules("required");
            $form->text('tokens','Tokens');
            $form->text('remark','Remark');

        });
    }

}
